/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Cash;
import br.com.foxline.clinicplus.entity.ContaBancaria;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class CashService extends GenericService<Cash> {

    public CashService() {
        super(Cash.class);
    }

    public Cash existCashToday(Date date) {
//        final Date date = new Date();
        Calendar gc = GregorianCalendar.getInstance();
        gc.setTime(date);
        gc.set(GregorianCalendar.HOUR_OF_DAY, 18);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);

        Date dateInitial = gc.getTime();
        Date dateFinal = null;

        String sql = "SELECT c FROM Cash AS c WHERE";

        if (dateInitial != null) {
            sql += " (c.creatAt = :dateinitial) and ";
        }

        sql += " c.closeCash = true and c.status = true";
        Query query = getEntityManager().createQuery(sql);

        if (dateInitial != null) {
            Calendar gc1 = GregorianCalendar.getInstance();
            gc1.setTime(dateInitial);
            
            if (gc1.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
                gc1.set(GregorianCalendar.HOUR_OF_DAY, 0);
            }

//            Calendar gc2 = GregorianCalendar.getInstance();
//
//            if (dateFinal == null) {
//                gc2.setTime(dateInitial);
//                gc2.add(GregorianCalendar.DAY_OF_MONTH, 1);
//                gc2.set(GregorianCalendar.HOUR_OF_DAY, 0);
//            } else {
//                gc2.setTime(dateFinal);
//                if (gc2.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
//                    gc2.set(GregorianCalendar.HOUR_OF_DAY, 23);
//                    gc2.set(GregorianCalendar.MINUTE, 59);
//                    gc2.set(GregorianCalendar.SECOND, 0);
//                }
//            }
            dateInitial = gc1.getTime();
//            dateFinal = gc2.getTime();
            query.setParameter("dateinitial", dateInitial);
//            query.setParameter("datefinal", dateFinal);
        }

        return query.getResultList().size() > 0 ? (Cash) query.getResultList().get(0) : null;
    }
    
    public BigDecimal saldoAnterior(ContaBancaria contaBancaria, Date data) {
        Calendar gc1 = GregorianCalendar.getInstance();
        gc1.setTime(data);
        gc1.add(Calendar.DAY_OF_MONTH, -1);
        return sumCash(contaBancaria, gc1.getTime());
    }

    public BigDecimal sumCash(ContaBancaria contaBancaria, Date data) {
        String sql = "SELECT SUM(c.value) FROM Cash AS c JOIN c.contaBancaria cb WHERE 1 = 1 ";
        if (contaBancaria != null && contaBancaria.getId() != null) {
            sql += " and cb.id = :contabancaria ";
        }
        if (data != null) {
            sql += " and c.creatAt <= :dateinitial ";
        }

        sql += " and c.status = true";
        Query query = getEntityManager().createQuery(sql);

        if (contaBancaria != null && contaBancaria.getId() != null) {
            query.setParameter("contabancaria", contaBancaria.getId());
        }
        if (data != null) {
            Calendar gc1 = GregorianCalendar.getInstance();
            gc1.setTime(data);
            gc1.set(GregorianCalendar.HOUR_OF_DAY, 23);
            gc1.set(GregorianCalendar.MINUTE, 59);
            gc1.set(GregorianCalendar.SECOND, 59);
            query.setParameter("dateinitial", gc1.getTime());

            System.err.println(gc1.getTime());
        }
        return (BigDecimal) query.getSingleResult();
    }

    public List<Cash> searchCash(Date dateInitial, Date dateFinal, ContaBancaria contaBancaria) {
        String sql = "SELECT c FROM Cash AS c JOIN c.contaBancaria cb WHERE";
        if (dateInitial != null) {
            sql += " (c.creatAt BETWEEN :dateinitial AND :datefinal) and ";
        }
        if (contaBancaria != null) {
            sql += " cb.id = :contaBancaria and ";
        }
        sql += " c.status = true order by c.creatAt";
        Query query = getEntityManager().createQuery(sql);

        if (dateInitial != null) {
            Calendar gc1 = GregorianCalendar.getInstance();
            gc1.setTime(dateInitial);

            if (gc1.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
                gc1.set(GregorianCalendar.HOUR_OF_DAY, 0);
            }

            Calendar gc2 = GregorianCalendar.getInstance();
            if (dateFinal == null) {
                gc2.setTime(dateInitial);
                gc2.add(GregorianCalendar.DAY_OF_MONTH, 1);
                gc2.set(GregorianCalendar.HOUR_OF_DAY, 0);
            } else {
                gc2.setTime(dateFinal);
                if (gc2.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
                    gc2.set(GregorianCalendar.HOUR_OF_DAY, 23);
                    gc2.set(GregorianCalendar.MINUTE, 59);
                    gc2.set(GregorianCalendar.SECOND, 0);
                }
            }
            dateInitial = gc1.getTime();
            dateFinal = gc2.getTime();
            if (dateInitial != null) {
                query.setParameter("dateinitial", dateInitial);
                query.setParameter("datefinal", dateFinal);
            }
        }

        if (contaBancaria != null) {
            query.setParameter("contaBancaria", contaBancaria.getId());
        }

        return (List<Cash>) query.getResultList();
    }

    public BigDecimal searchValueCash(Date dateInitial, Date dateFinal, ContaBancaria contaBancaria, Cash c) {
        String sql = "SELECT SUM(c.value) FROM Cash AS c JOIN c.contaBancaria cb WHERE";
        if (dateInitial != null) {
            sql += " (c.creatAt BETWEEN :dateinitial AND :datefinal) and ";
        }
        if (contaBancaria != null) {
            sql += " cb.id = :contaBancaria and ";
        }
        if (c != null && c.getTypeIncome() != null) {
            sql += " c.typeIncome =  :typeincome and ";
        }
        sql += " c.status = true";
        Query query = getEntityManager().createQuery(sql);

        if (c != null && c.getTypeIncome() != null) {
            query.setParameter("typeincome", c.getTypeIncome());
        }
        if (dateInitial != null) {
            Calendar gc1 = GregorianCalendar.getInstance();
            gc1.setTime(dateInitial);

            if (gc1.get(GregorianCalendar.HOUR_OF_DAY) != 0) {
                gc1.set(GregorianCalendar.HOUR_OF_DAY, 0);
            }

            Calendar gc2 = GregorianCalendar.getInstance();

            if (dateFinal == null) {
                gc2.setTime(dateInitial);
                gc2.add(GregorianCalendar.DAY_OF_MONTH, 1);
                gc2.set(GregorianCalendar.HOUR_OF_DAY, 0);
            } else {
                gc2.setTime(dateFinal);
                if (gc2.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
                    gc2.set(GregorianCalendar.HOUR_OF_DAY, 23);
                    gc2.set(GregorianCalendar.MINUTE, 59);
                    gc2.set(GregorianCalendar.SECOND, 0);
                }
            }
            dateInitial = gc1.getTime();
            dateFinal = gc2.getTime();
            query.setParameter("dateinitial", dateInitial);
            query.setParameter("datefinal", dateFinal);
        }

        if (contaBancaria != null) {
            query.setParameter("contaBancaria", contaBancaria.getId());
        }

        BigDecimal resul = (BigDecimal) query.getSingleResult();
        if (resul == null) {
            return BigDecimal.ZERO;
        }

        return resul;
    }
    public BigDecimal searchValueCashComIncome(Date dateInitial, Date dateFinal, ContaBancaria contaBancaria, Cash c) {
        String sql = "SELECT SUM(c.value) FROM Income i JOIN i.cash AS c JOIN c.contaBancaria cb WHERE";
        if (dateInitial != null) {
            sql += " (c.creatAt BETWEEN :dateinitial AND :datefinal) and ";
        }
        if (contaBancaria != null) {
            sql += " cb.id = :contaBancaria and ";
        }
        if (c != null && c.getTypeIncome() != null) {
            sql += " c.typeIncome =  :typeincome and ";
        }
        sql += " c.status = true";
        Query query = getEntityManager().createQuery(sql);

        if (c != null && c.getTypeIncome() != null) {
            query.setParameter("typeincome", c.getTypeIncome());
        }
        if (dateInitial != null) {
            Calendar gc1 = GregorianCalendar.getInstance();
            gc1.setTime(dateInitial);

            if (gc1.get(GregorianCalendar.HOUR_OF_DAY) != 0) {
                gc1.set(GregorianCalendar.HOUR_OF_DAY, 0);
            }

            Calendar gc2 = GregorianCalendar.getInstance();

            if (dateFinal == null) {
                gc2.setTime(dateInitial);
                gc2.add(GregorianCalendar.DAY_OF_MONTH, 1);
                gc2.set(GregorianCalendar.HOUR_OF_DAY, 0);
            } else {
                gc2.setTime(dateFinal);
                if (gc2.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
                    gc2.set(GregorianCalendar.HOUR_OF_DAY, 23);
                    gc2.set(GregorianCalendar.MINUTE, 59);
                    gc2.set(GregorianCalendar.SECOND, 0);
                }
            }
            dateInitial = gc1.getTime();
            dateFinal = gc2.getTime();
            query.setParameter("dateinitial", dateInitial);
            query.setParameter("datefinal", dateFinal);
        }

        if (contaBancaria != null) {
            query.setParameter("contaBancaria", contaBancaria.getId());
        }

        BigDecimal resul = (BigDecimal) query.getSingleResult();
        if (resul == null) {
            return BigDecimal.ZERO;
        }

        return resul;
    }
}
